package b1.mobile.blackberry.util;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

public class JSONUtil {
	JSONObject _obj;
	public JSONUtil(JSONObject obj){
		_obj = obj; 
	}
	
	public static String getString(JSONObject obj, String key){
		String value;
		try{
			value = obj.getString(key);
		} catch(Exception ex){
			value = "";
		}
		
		return value;
	}
	
	public static String getStringElementByIndexFromJSONArray(JSONArray array, int index){
		String value = null;
		try{
			value = (String)array.get(index);
		} catch (Exception ex){
			value = "";
		}
		return value;
	}
	
	public String getString(String key){
		String value;
		try{
			value = _obj.getString(key);
		} catch(Exception ex){
			value = "";
		}
		
		return value;
	}
	
	public JSONArray getArray(String key){
		JSONArray value;
		try{
			value = _obj.getJSONArray(key);
		} catch(Exception ex){
			value = null;
		}
		return value;
	}

	public static void putOpt(JSONObject obj, String key, String value) {
		try {
			obj.putOpt(key, value);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
